//cc.Class({
const { ccclass, property } = cc._decorator;
// import { ResMgr } from "./ResManger";
// import { timerUtil } from "./TimerFactory";
import { audioUtil } from "./AudioUtil";
//import { SceneMgr } from "./../../scene/sceneMgr";
import { glConfig } from "./../Config/GLConfig";
import { glDefine } from "./../Config/GLDefine";
import { UIMgr } from "./../lib/UIMgr";
import { platform } from "./../Config/Platform";
import { glSDKMgr } from "./../lib/GLSDKMgr";
import { glHttpUtils } from "./../lib/GLHttpUtils";
import { glNetDataModel } from "./../lib/GLNetDataModel";
import { glSettingDataModel } from "./../lib/GLSettingDataModel";
import { glUtils } from "./../lib/GLUtils";
import { uuid } from "./../lib/UUID";
import { glNet } from "./../Login/Net/GLNet";
import { eventCustom } from "./../lib/EventCustom";
import { glCusStorage } from "./GLCusStorage";
import { poolUtil } from "./../lib/Pool";
import { core } from "./../lib/core";
import { timerUtil } from "./../lib/TimerFactory";
import { serverList } from "./../Config/ServerList";
import { hotUpdate } from "./../Common/HotUpdate/HotUpdate";
import { baseDispatch } from "./../lib/Network/BaseDispatch";
import { protobufTools } from "./../lib/Network/ProtobufTools";
import netgamehall from "./../Hall/Network/GameHallNetwork";
import { gameChooseConfig } from "./../Hall/GameChooseConfig";
import { resLoader } from "./../lib/ResLoader";
import { voiceNative } from "./../lib/VoiceNative";
import { marqueeDataModel } from "./../Marquee/MarqueeDataModel";
import { wxChat } from "./../lib/WXChat";
import { session } from "../Hall/Session";
import { dataConfig } from "./util/dataConfig";
import { charDataMgr } from "./Chat/ChatDataMgr";


@ccclass
export default class InitSys extends cc.Component {
	@property(cc.Node)
	other: cc.Node = null;
	OrientationChangeevent = null;
	openChatevent = null;
	onLoad() {

		if (glConfig && glConfig.isInit) {
			cc.vv.UIMgr.init(this.other);
			cc.vv.GLUtils.init();
			return;
		}

		this.initAllModules();

		this.addGameEvent();

		if (!cc.sys.isNative) {
			var id = this.getQueryString("id");
			cc.vv.session.setId(id);
		}

	}
	onDestroy() {
		if (this.openChatevent != "undefined") {
			cc.vv.eventCustom.off("openChat", this.openChatevent);
			this.openChatevent = undefined;
		}
		if (this.OrientationChangeevent != "undefined") {
			cc.vv.eventCustom.off("OrientationChange", this.OrientationChangeevent);
			this.OrientationChangeevent = undefined;
		}
	}
	update(dt) { }
	getQueryString(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			return unescape(r[2]);
		}
		return null;
	}


	/**
	 * 初始化所有模块
	 */
	initAllModules() {
		if (!cc.vv) {
			cc.vv = {};
		}

		cc.vv.core = core;

		cc.vv.GLDefine = glDefine;
		cc.vv.platform = platform;

		cc.vv.GLConfig = glConfig;
		cc.vv.GLConfig.init();

		cc.vv.session = session;
		cc.vv.session.init();

		cc.vv.UIMgr = UIMgr;
		cc.vv.UIMgr.init(this.other);

		cc.vv.GLNet = glNet;

		cc.vv.resLoader = resLoader;

		// cc.vv.GroupDefine = require("GroupDefine");

		// cc.vv.base64Tool = require("base64Tool");
		// cc.vv.base64Tool.init();

		cc.vv.audioUtil = audioUtil;
		cc.vv.audioUtil.init();

		cc.vv.uuid = uuid;
		cc.vv.uuid.init();

		// cc.vv.GLSceneMgr = require("GLSceneMgr");
		// cc.vv.GLSceneMgr.init();

		// cc.vv.ErrorCodeDataModel = require("ErrorCodeDataModel");

		// cc.vv.EmojDataModel = require("EmojDataModel");
		cc.vv.GLNetDataModel = glNetDataModel;
		cc.vv.GLNetDataModel.init();

		cc.vv.GLHttpUtils = glHttpUtils;
		cc.vv.GLHttpUtils.init();

		cc.vv.baseDispatch = baseDispatch;

		// var GlobalMsg = require("GlobalMsg");
		// cc.vv.GlobalMsg = new GlobalMsg();
		// cc.vv.GlobalMsg.init();

		cc.vv.GLCusStorage = glCusStorage;
		cc.vv.GLCusStorage.init();

		// cc.vv.GLPageDataModel = require("GLPageDataModel"); //页面数据缓存;
		// cc.vv.GLPageDataModel.init();

		// cc.vv.GLAudioTool = require("GLAudioTool");

		cc.vv.GLSettingDataModel = glSettingDataModel;
		cc.vv.GLSettingDataModel.init();

		cc.vv.serverList = serverList;

		cc.vv.eventCustom = eventCustom;
		cc.vv.Timer = timerUtil;
		cc.vv.Timer.init(this.node);

		cc.vv.Pool = poolUtil;

		// cc.vv.GLPageMgr = require("GLPageMgr");
		cc.vv.GLUtils = glUtils;
		cc.vv.GLUtils.init();

		// cc.vv.GLTextMgr = require("GLTextMgr");
		// cc.vv.GLTextMgr.init();

		// cc.vv.rechargeConfig = rechargeConfig;

		cc.vv.proto_filter = protobufTools;

		// //todo: 热更管理;
		cc.vv.hotUpdate = hotUpdate;

		cc.vv.chatMgr = charDataMgr;
		cc.vv.chatMgr.init();

		// // cc.vv.GLResMgr = require("GLResMgr");
		cc.vv.wxChatMgr = wxChat;
		cc.vv.wxChatMgr.init();

		// cc.vv.ClubConfig = clubConfig;
		// var sdk = require("GLSDKMgr");
		cc.vv.GLSDKMgr = glSDKMgr;
		cc.vv.GLSDKMgr.init();

		cc.vv.netgamehall = netgamehall;

		cc.vv.GameChooseConfig = gameChooseConfig;
		cc.vv.GameChooseConfig.init();

		// cc.vv.createRoomConfig = createRoomConfig;
		// cc.vv.createRoomConfig.init();

		// cc.vv.GLGameStateMgr = require('GLGameStateMgr');
		// cc.vv.GLGameStateMgr.initListener();


		// // 邮件数据中心;进入大厅后初始化;
		// cc.vv.MailDataModel = new MailDataModel();
		// cc.vv.MailDataModel.init();
		// 服务器配置信息 暂只有fqzs 预留模块
		//cc.vv.GLServerCfg = GLSeverCfg;

		// // 跑马灯;
		cc.vv.MarqueeDataModel = marqueeDataModel;
		cc.vv.MarqueeDataModel.init();

		// // 服务器地址
		// var serverList = require('GLServerListMgr');
		// cc.vv.GLServerListMgr = new serverList();



		// //get versition type
		var _versionType = cc.vv.GLSDKMgr.getVersionType();
		cc.vv.GLNetDataModel.setVersionType(_versionType);
		if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
			if (cc.vv.GLConfig.isIosExaminePackage) {
				cc.vv.GLNetDataModel.setPlatformCfg(cc.vv.serverList.cdn_address)
			}
		}
		//cc.vv.sceneMgr = SceneMgr;

		// cc.vv.ExchangeDataModel = new ExchangeDataModel();
		// //初始化反馈数据模块
		// cc.vv.FeedbackDataModel = new FeedbackDataModel();
		// cc.vv.FeedbackDataModel.init();

		//语音模块
		cc.vv.voiceNative = voiceNative;
		cc.vv.voiceNative.init();

		cc.vv.dataCfg = dataConfig.getInstance();

		// cc.vv.AgencyDataModel = require("AgencyDataModel");
		// cc.vv.AgencyDataModel.init();

		// cc.vv.AdaptationOffset = require("AdaptationOffset");

		//cc.vv.AllAgencyConfig = allAgencyConfig;
		// cc.vv.AllAgencyDataModel = new AllAgencyDataModel();
		// cc.audioEngine.setMaxAudioInstance(24);
		if (cc.sys.os == cc.sys.OS_IOS) {
			let model = false
			model = jsb.reflection.callStaticMethod(
				"AppController",
				"getIOScheckhot"
			);
			if (model) {
				cc.vv.GLConfig.getIOScheckhot = model;
			}
		}
		//获取软键盘高度
		if (cc.sys.isNative) {
			var softkeyboardreal = Number(cc.sys.localStorage.getItem("softkeyboardreal"))
			if (softkeyboardreal > 0) {
				if (cc.sys.os == cc.sys.OS_ANDROID) {
					jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "setsoftkeoardheight", "(I)V", softkeyboardreal);
				} else if (cc.sys.os == cc.sys.OS_IOS) {
					console.log("js 设置软键盘高度", softkeyboardreal)
					jsb.reflection.callStaticMethod("AppController", "setsoftkeoardheight:", softkeyboardreal);
				}
			}
		}
	}

	/**
	 * 后台事件;
	 */
	addGameEvent() {
		cc.game.on(
			cc.game.EVENT_HIDE,
			function () {
				console.log("游戏进入后台");
				cc.vv.eventCustom.emit("L_hideGame");
			}.bind(this)
		);
		cc.game.on(
			cc.game.EVENT_SHOW,
			function () {
				console.log("游戏返回前台");
				if (cc.sys.os == cc.sys.OS_IOS) {
					if (cc.vv.GLConfig.currentOrientation == 1) {
						cc.vv.GLUtils.changeCanvasToHorizontal()
					}
					if (cc.vv.GLConfig.currentOrientation == 0) {
						cc.vv.GLUtils.changeCanvasToVertical()
					}
				}
				cc.vv.eventCustom.emit("L_showGame");
			}.bind(this)
		);
		this.openChatevent = cc.vv.eventCustom.on("openChat", this.openChat.bind(this), this.node);
		this.OrientationChangeevent = cc.vv.eventCustom.on("OrientationChange", this.OrientationChange.bind(this), this.node);
	}
	openChat(data) {
		let frienddata = data[0].friendinfo;
		let from = data[0].from;
		cc.vv.chatMgr.setCurrentOpenData(frienddata);
		if (frienddata.isClub && from == 0) {
			cc.vv.session.setCurClub(frienddata);
			cc.vv.UIMgr.open(cc.vv.GLConfig.gameHall_path, null, () => {
				cc.vv.eventCustom.emit("chooseGameHallItem", cc.vv.GLConfig.gameHallUIType.huiHua);
			});
		} else {
			cc.vv.UIMgr.open(cc.vv.GLConfig.chat_path, this.other, null, frienddata, from);
		}
	}
	OrientationChange(data) {
		// console.log("从原生端抛上来的事件data  is ***", data)
		// let Orientation = Number(data);
		// if (Orientation == 0) {  //未知方向
		// 	if (cc.vv.GLConfig.currentOrientation == 1) {//ui横屏
		// 		console.log("从原生端抛上来的方向是未知  但是ui是横屏  切换到横屏 ***")
		// 		cc.vv.GLUtils.changeCanvasToHorizontal();
		// 	}
		// 	if (cc.vv.GLConfig.currentOrientation == 0) { //ui竖屏
		// 		console.log("从原生端抛上来的方向是未知  但是ui是竖屏  切换到竖屏 ***")
		// 		cc.vv.GLUtils.changeCanvasToVertical();
		// 	}
		// }
		// if (Orientation == 1 || Orientation == 2) {  //竖屏方向
		// 	if (cc.vv.GLConfig.currentOrientation == 1) {//ui横屏
		// 		console.log("从原生端抛上来的方向是竖屏  但是ui是横屏  切换到横屏 ***")
		// 		cc.vv.GLUtils.changeCanvasToHorizontal();
		// 	}
		// }
		// if (Orientation == 3 || Orientation == 4) {  //横屏方向
		// 	if (cc.vv.GLConfig.currentOrientation == 0) { //ui竖屏
		// 		console.log("从原生端抛上来的方向是横屏  但是ui是竖屏  切换到竖屏 ***")
		// 		cc.vv.GLUtils.changeCanvasToVertical();
		// 	}
		// }
	}
	/**
	 * 修复按钮点击;
	 */
	Btn_Repair_OnClick() {
		cc.vv.GLUtils.cleanCache();
	}
}

//重写editbox的方法  增加空判断
cc.EditBox.prototype.editBoxEditingDidBegan = function () {
	if (this && this.editingDidBegan) {
		cc.Component.EventHandler.emitEvents(this.editingDidBegan, this);
		this.node.emit('editing-did-began', this);
	}
}
cc.EditBox.prototype.editBoxEditingDidEnded = function () {
	if (this && this.editingDidEnded) {
		cc.Component.EventHandler.emitEvents(this.editingDidEnded, this);
		this.node.emit('editing-did-ended', this);
	}
}

cc.EditBox.prototype.editBoxEditingReturn = function () {
	if (this && this.editingReturn) {
		cc.Component.EventHandler.emitEvents(this.editingReturn, this);
		this.node.emit('editing-return', this);
	}
}

export let initSys: InitSys = new InitSys();
